﻿using FFLTask.BLL.Entity;
using FFLTask.GLB.Global.Enum;
using Global.Core.ExtensionMethod;
using Global.Core.Helper;
using NUnit.Framework;
using System.Linq;

namespace FFLTask.BLL.EntityTest
{
    public static class TaskTestHelper
    {
        internal static HistoryItem get_latest_history(this Task task)
        {
            if (!task.Histories.IsNullOrEmpty())
            {
                HistoryItem last_desciption = task.Histories
                    .LastOrDefault(h => h.Description != Constants.DescriptionComment)
                    ;
                if (last_desciption != null)
                {
                    return last_desciption;
                }
                else
                {
                    throw new AssertionException("no desciption in the history.");
                }
            }
            else
            {
                throw new AssertionException("no history in the task.");
            }
        }

        internal static HistoryItem get_latest_comment(this Task task)
        {
            if (!task.Histories.IsNullOrEmpty())
            {
                HistoryItem last_comment = task.Histories
                    .LastOrDefault(h => h.Description == Constants.DescriptionComment)
                    ;
                if (last_comment != null)
                {
                    return last_comment;
                }
                else
                {
                    throw new AssertionException("no comment in the history.");
                }
            }
            else
            {
                throw new AssertionException("no history in the task.");
            }
        }

        internal static void has_comment(this Task task, string comment)
        {
            Assert.That(task.get_latest_comment().Comment, Is.EqualTo(comment));
        }

        internal static void has_update_latest(this Task task)
        {
            Assert.That(task.LatestUpdateTime, Is.EqualTo(SystemTime.Now()));
        }

        internal static void has_begin(this Task task)
        {
            Assert.That(task.CurrentStatus, Is.EqualTo(Status.BeginWork));
            task.has_update_latest();
            Assert.That(task.get_latest_history().Description, Is.EqualTo(Constants.DescriptionBeginWork));
        }

        internal static void has_accept(this Task task)
        {
            task.has_accept(null);
        }

        internal static void has_accept(this Task task, TaskQuality? quality)
        {
            Assert.That(task.HasAccepted, Is.EqualTo(true));
            Assert.That(task.CurrentStatus, Is.EqualTo(Status.Accept));
            Assert.That(task.Quality, Is.EqualTo(quality));

            task.has_update_latest();

            HistoryItem latest = task.get_latest_history();
            Assert.That(latest.Description, Is.EqualTo(Constants.DescriptionAccept));
        }

        internal static void has_complete(this Task task)
        {
            Assert.That(task.CurrentStatus, Is.EqualTo(Status.Complete));
            task.has_update_latest();
            Assert.That(task.get_latest_history().Description, 
                Is.EqualTo(Constants.DescriptionComplete));
        }
    }
}
